<?php
/**
 * Created by PhpStorm.
 * User: 1
 * Date: 2018/3/23
 * Time: 13:22
 */

namespace app\api\model;
use app\common\lib\exception\ApiException;
use think\Model;
use think\Db;
use think\Log;

class Classroom extends Model
{
    /**
     * 通过id查询教室记录
     * param status int 必传 1用户查询教室推荐记录 2 用户查询教室归属记录
     * @author 巫玉梅 <1120504614@qq.com>
     * @return false|\PDOStatement|string|\think\Collection
     */
   
    public function getClassroomById($data){

        $where = array();
        if($data['status'] == 1){
            $where=array('rec_classroom_user'=>$data['id']);
        }elseif($data['status'] == 2){
            $where=array('classroom_owner'=>$data['id']);
        }

        $info =Db::name('classroom')
            ->alias("classroom")
            ->where($where)
            ->join('user','classroom_owner=user.id','LEFT')
            ->order('classroom.id desc')
            ->field('classroom_sn,classroom_address,
                    user.full_name,user.tel')
            ->select();

        return $info;
    }

    /**
     * @获取 教室信息列表
     * @author 巫玉梅 <1120504614@qq.com>
     * @return ApiException|array|string|\think\response\Json
     */
    public function getAllClassroom(){
        try{
            $info = Db::name('classroom')->select();
            return show(config('code.success'),'获取数据成功',$info,200);

        }catch(\Exception $e){
            Log::write($e->getMessage());
            throw new ApiException($e->getMessage(),400);
        }
    }

    /**
     * @param查询某人推荐的教室记录或者查询某人所拥有的教室记录
     * @param status int 必传 1 查询某人推荐的教室记录 2查询某人所拥有的教室记录
     * @param id int 用户id 必传
     * @author 巫玉梅 <1120504614@qq.com>
     */
    public function recOrOwnClassroom(){
        $data = $_POST;

        if(empty($data)){
            throw new ApiException('数据不合法',404);
        }
        //user_id 用户id 必传
        if(empty($data['id'])){
            throw new ApiException('id is not found',404);
        }
        if(empty($data['status'])){
            throw new ApiException('status is not found',404);
        }
        try{

            $info = $this->getClassroomById($data);

            return show(config('code.success'),'获取数据成功',$info,200);

        }catch(\Exception $e){
            Log::write($e->getMessage());
            throw new ApiException($e->getMessage(),400);
        }
    }




}